package com.icewld.meiflowerway;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.Serializable;
import java.nio.InvalidMarkException;

public class ResultViewer extends AppCompatActivity {

    ImageView[] rootYaoView   = new ImageView[6];
    ImageView[] doingYaoView  = new ImageView[6];
    ImageView[] changeYaoView = new ImageView[6];
    TextView rootName, doingName, changeName;
    TextView detailGua;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_viewer);

        rootYaoView[0]   = (ImageView)findViewById(R.id.imageView2);
        rootYaoView[1]   = (ImageView)findViewById(R.id.imageView3);
        rootYaoView[2]   = (ImageView)findViewById(R.id.imageView);
        rootYaoView[3]   = (ImageView)findViewById(R.id.imageView10);
        rootYaoView[4]   = (ImageView)findViewById(R.id.imageView11);
        rootYaoView[5]   = (ImageView)findViewById(R.id.imageView12);
        doingYaoView[0]  = (ImageView)findViewById(R.id.imageView6);
        doingYaoView[1]  = (ImageView)findViewById(R.id.imageView5);
        doingYaoView[2]  = (ImageView)findViewById(R.id.imageView4);
        doingYaoView[3]  = (ImageView)findViewById(R.id.imageView13);
        doingYaoView[4]  = (ImageView)findViewById(R.id.imageView14);
        doingYaoView[5]  = (ImageView)findViewById(R.id.imageView15);
        changeYaoView[0] = (ImageView)findViewById(R.id.imageView9);
        changeYaoView[1] = (ImageView)findViewById(R.id.imageView8);
        changeYaoView[2] = (ImageView)findViewById(R.id.imageView7);
        changeYaoView[3] = (ImageView)findViewById(R.id.imageView16);
        changeYaoView[4] = (ImageView)findViewById(R.id.imageView17);
        changeYaoView[5] = (ImageView)findViewById(R.id.imageView18);

        rootName = (TextView)findViewById(R.id.textView9);
        doingName = (TextView)findViewById(R.id.textView10);
        changeName = (TextView)findViewById(R.id.textView11);

        detailGua = (TextView)findViewById(R.id.textView8);

        Intent intent = getIntent();
        Serializable se = intent.getSerializableExtra("Cal");
        if (se instanceof MainCalModel){
            MainCalModel mcm = (MainCalModel) se;
            mcm.GetGua();
            DisplayGua(mcm);
            DisplayName(mcm.ReadNameAndYao(1, this.getBaseContext())[0], mcm.ReadNameAndYao(2,this.getBaseContext())[0],
                    mcm.ReadNameAndYao(3, this.getBaseContext())[0]);
            DisplayYao(mcm.ReadNameAndYao(1, this.getBaseContext())[1] + "\n" +mcm.ReadNameAndYao(3, this.getBaseContext())[1]);
        }
    }

    void DisplayGua(MainCalModel m){
        int[] rootYao   = new int[6];
        int[] doingYao  = new int[6];
        int[] changeYao = new int[6];
        for (int i = 0; i < 6; i++){
            if (i < 3){
                rootYao[i]   = m.GetYao(m.getRoot_upGua())[i];
                doingYao[i]  = m.GetYao(m.getDoing_upGua())[i];
                changeYao[i] = m.GetYao(m.getChange_upGua())[i];
            }
            else {
                rootYao[i] = m.GetYao(m.getRoot_downGua())[i-3];
                doingYao[i]  = m.GetYao(m.getDoing_downGua())[i-3];
                changeYao[i] = m.GetYao(m.getChange_downGua())[i-3];
            }
            if (rootYao[i] == 1){rootYaoView[i].setImageResource(R.drawable.yang);}
            else {rootYaoView[i].setImageResource(R.drawable.yin);}
            if (doingYao[i] == 1){doingYaoView[i].setImageResource(R.drawable.yang);}
            else {doingYaoView[i].setImageResource(R.drawable.yin);}
            if (changeYao[i] == 1){changeYaoView[i].setImageResource(R.drawable.yang);}
            else {changeYaoView[i].setImageResource(R.drawable.yin);}
        }
    }

    void DisplayName(String root_name, String doing_name, String change_name){
        rootName.setText(root_name); doingName.setText(doing_name); changeName.setText(change_name);
    }

    void DisplayYao(String yaoWord){
        detailGua.setText(yaoWord);
    }
}
